﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using PPAP.Model;

namespace PPAP.DAO
{
    public class SupplierAdminDAO
    {
        public SupplierAdminDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SupplierAdmin model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "SUPPLIERADMIN");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SUPPLIERADMIN(");
            strSql.Append("ID,USERNAME,PASSWORD,SUPPLIERID)");
            strSql.Append(" values (");
            strSql.Append(":ID,:USERNAME,:PASSWORD,:SUPPLIERID)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":PASSWORD", OracleType.VarChar,50),
					new OracleParameter(":SUPPLIERID", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.USERNAME.ToLower();
            parameters[2].Value = model.PASSWORD;
            parameters[3].Value = model.SUPPLIERID;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(SupplierAdmin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SUPPLIERADMIN set ");
            strSql.Append("ID=:ID,");
            strSql.Append("USERNAME=:USERNAME,");
            strSql.Append("PASSWORD=:PASSWORD,");
            strSql.Append("SUPPLIERID=:SUPPLIERID");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":PASSWORD", OracleType.VarChar,50),
					new OracleParameter(":SUPPLIERID", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.USERNAME.ToLower();
            parameters[2].Value = model.PASSWORD;
            parameters[3].Value = model.SUPPLIERID;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SUPPLIERADMIN ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SupplierAdmin GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,USERNAME,PASSWORD,SUPPLIERID from SUPPLIERADMIN ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            SupplierAdmin model = new SupplierAdmin();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                model.PASSWORD = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                if (ds.Tables[0].Rows[0]["SUPPLIERID"].ToString() != "")
                {
                    model.SUPPLIERID = int.Parse(ds.Tables[0].Rows[0]["SUPPLIERID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,USERNAME,PASSWORD,SUPPLIERID ");
            strSql.Append(" FROM SUPPLIERADMIN ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取供应商管理员列表
        /// </summary>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public DataSet GetListForbyd(string strwhere)
        {
            string sql = "select rownum,a.username,s.suppliername,s.suppliercode,s.id from supplieradmin a left join supplier s on a.supplierid=s.id";
            if (strwhere.Trim() != "")
            {
                sql +=" where "+ strwhere;
            }
            return DbHelperOra.Query(sql);
        }

    }
}
